package com.xiangshizhineng.tianzhangzhi.app.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangshizhineng.tianzhangzhi.app.model.dto.TaskDTO;
import com.xiangshizhineng.tianzhangzhi.app.model.request.PatrolRequest;
import com.xiangshizhineng.tianzhangzhi.app.model.request.TaskRequest;
import com.xiangshizhineng.tianzhangzhi.app.model.response.*;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface TaskDAO extends BaseMapper<TaskResponse> {

    /**
     * 当前用户催办的任务集合
     *
     */
    List<TaskResponse> urgeTask(Long id);

    /**
     * 催办任务详情
     *
     */
    TaskResponse cbTaskDetail(Long id,Long fId);

    /**
     * 图斑查看任务列表
     *
     */
    List<TaskResponse> tbTask(Long fId);

    /**
     * 开始任务
     */
    int startTask(TaskRequest taskRequest);

    /**
     * 检查任务是否被他人开始
     */
    String isStart(TaskRequest taskRequest);

    /**
     * 结束任务
     */
    int endTask(PatrolRequest patrolRequest);

    /**
     * 任务新消息
     */
    int maxId(String userId);

    /**
     * 催办任务新消息
     */
    MesIsMaxIdResponse cbMaxId(String userId);

    /**
     * 判断是否是新任务
     */
    int isMaxId(String userId);

    /*
     * 查询图斑id
     * */
    List<String> selectTbId(Long userId,String onPatrol,String patrolled,String toBeInspected);


    /**
     * 任务菜单任务详情
     *
     */
    TaskResponse hisTaskDetail(Long id);

    /**
     * 网格员查询上级田长
     */
    List<UserResponse> selectTz(String xzqdm);

    /**
     * 三级田长查看上级田长
     */
    List<UserResponse> selectTzT(String xzqdm);

    /**
     * 查看田长极别
     */
    String selectLevel(Long tbId);

    /**
     * 查看图斑id全部
     */
    List<String> selectTbIdAll(Long userId);

    /**
     * 查看新消息提醒
     */
    int selectNew(String userId);

    /**
     * 检查是否有开始任务
     */
    int isHasTask(String userId);

    /**
     * 首页查看任务查询下发图斑，上报事件类型的催办任务
     * @param params
     * @return
     */
    List<TaskResponse> mainTaskHis(Map<String, Object> params);

    /**
     * 催办历史任务详情
     */
    TaskResponse cbTaskDetailHis(Long id,Long fId);

    /**
     * 检查历史任务表是否有开始任务
     */
    int isHasTaskHis(String userId);

    /**
     * 检查历史任务是否被他人开始
     */
    String isStartHis(TaskRequest taskRequest);

    /**
     * 历史表开始任务
     */
    int startTaskHis(TaskRequest taskRequest);

    /**
     * 我的任务列表分页
     */
    @Select("SELECT * FROM (SELECT * FROM tz_task union select * from tz_task_history where task_status_id <> '20'" +
            "and task_type_id in('16','17')) t ${ew.customSqlSegment}")
    IPage<TaskResponse> selectByMyWrapper(@Param("page") Page page, @Param(Constants.WRAPPER) Wrapper<TaskResponse> userWrapper);

    /**
     * 首页查看催办任务
     * @param page
     * @param userWrapper
     * @return
     */
    @Select("SELECT task_name,task_s_time,task_e_time,task_status_id,cb,id FROM (SELECT * FROM tz_task union select * from tz_task_history) t ${ew.customSqlSegment}")
    IPage<TaskResponse> selectCbByMyWrapper(@Param("page") Page page, @Param(Constants.WRAPPER) Wrapper<TaskResponse> userWrapper);

    /**
     * 全部任务列表分页
     */
    @Select("SELECT * FROM (SELECT * FROM tz_task union select * from tz_task_history where task_status_id <> '20'" +
            "and task_type_id in('16','17') and task_xcr is null) t ${ew.customSqlSegment}")
    IPage<TaskResponse> selectByAllWrapper(@Param("page") Page page, @Param(Constants.WRAPPER) Wrapper<TaskResponse> userWrapper);
    /**
     * 统计面积
     */
    @Select("SELECT sum(mj) mj FROM tz_task ${ew.customSqlSegment}")
    Double sumMj(@Param(Constants.WRAPPER) Wrapper<TaskResponse> taskResponseWrapper);
    /**
     * 全部任务 任务现状&历史任务
     */
    @Select("SELECT * FROM (select * from tz_task union select * from tz_task_history) t ${ew.customSqlSegment}")
    List<TaskResponse> allTasks(@Param(Constants.WRAPPER) Wrapper<TaskResponse> taskResponseWrapper);

    /**
     * 根据id查询任务（现状表&历史表）
     * @param taskResponseWrapper
     * @return
     */
    @Select("SELECT * FROM (select * from tz_task union select * from tz_task_history) t ${ew.customSqlSegment}")
    TaskResponse allTasksById(@Param(Constants.WRAPPER) Wrapper<TaskResponse> taskResponseWrapper);

    /**
     * 村任务完成情况
     */
    @Select("select v.`name` name ,sum(case when t.task_status_id= 20 then 1 else 0 end) taskCompleted, sum(case when t.task_status_id= 18 then 1 else 0 end) taskCompleting,\n" +
            "sum(case when t.task_status_id= 21 then 1 else 0 end) taskTimeOut,sum(case when t.task_status_id !=0 then 1 else 0 end) allTasks, \n" +
            "(case when sum(case when t.task_status_id= 20 then 1 else 0 end)!=0 then\n" +
            "ROUND(sum(case when t.task_status_id= 20 then 1 else 0 end)/sum(case when t.task_status_id !=0 then 1 else 0 end)*100,2) else 0 end) taskCompletion,\n" +
            "sum(case when e.event_status_id != 0 then 1 else 0 end) reported,\n" +
            "sum(case when e.event_status_id != 12 then 1 else 0 end) eventCompleting,sum(case when e.event_status_id= 12 then 1 else 0 end) eventCompleted,\n" +
            "sum(case when e.event_status_id !=0 then 1 else 0 end) allEvents,\n" +
            "(case when sum(case when e.event_status_id= 12 then 1 else 0 end)!=0 then \n" +
            "ROUND(sum(case when e.event_status_id= 12 then 1 else 0 end)/sum(case when e.event_status_id !=0 then 1 else 0 end)*100,2) else 0 end) eventCompletion\n" +
            "from (select * from tz_task union select * from tz_task_history) t left join tz_district v on t.xzqhdm = v.code left join tz_event e on t.id = e.task_id " +
            "${ew.customSqlSegment} group by v.name")
    List<CompletionResponse> allChild(@Param(Constants.WRAPPER) Wrapper<DistrictResponse> districtResponseWrapper);

    @Select("SELECT COUNT(1) FROM (select * from tz_task where task_status_id in ('18','19') union select * from tz_task_history where task_status_id <> '20' " +
            "and task_type_id in('16','17')) t ${ew.customSqlSegment}")
    int allTasksByIdCountMes(@Param(Constants.WRAPPER) Wrapper<TaskResponse> taskResponseWrapper);
}
